<?php
namespace Home\Controller;
use Home\Controller\UserController;
use Common\Model\MemberModel;
class TradeController extends UserController {
	public function _initialize(){
		parent::_initialize();
	}
	public function getTagByUid(){
	    $list = M('Member_tag')->where(['uid'=>$this->uid])->find();
	    $return['code'] = $list['code'];
	    $return['status'] = 1;
	    $this->ajaxReturn($return);
	}
	public function getUserMoneyByType(){
	    $type = I('type');
	    $list = M('Member_money')->where(['member_id'=>$this->uid])->find();
 	    $list['num'] = $list['money'.$type];
	    $return['status'] = 1;
	    $this->ajaxReturn($return);
	}
	//获取钱包地址
	public function getWalletAddress (){
	    $type = I('currency_id');
	    $currency=M('Currency')->where('currency_id ='.$type)->find();
	    $return['currency'] = $currency;
	    //$type = $type+1;
	    if(!$type){
	        $return['info'] = '参数错误';
	        $return['status'] = -1;
	        $this->ajaxReturn($return);
	    }
	    $uid = $this->uid;
	    $coin_address = M('Wallet')->where(['uid'=>$uid])->find();
	    //如果点进来的是btc 数据库里已经有地址直接取数据库的值
	    if($coin_address&&$coin_address['btc_address']&&$type==1){
	        //echo 1;
            $data['url'] = $coin_address['btc_address'];
            $data['ercode'] = $coin_address['btc_ercode'];
            $return['data'] = $data;
            $return['status'] = 1;
            $this->ajaxReturn($return);
            //如果点进来的是eth 数据库里已经有地址直接取数据库的值
	    }elseif($coin_address&&$coin_address['eth_address']&&$type==2){
	        //echo 2;
	        $data['url'] = $coin_address['eth_address'];
	        $data['ercode'] = $coin_address['eth_ercode'];
	        $return['data'] = $data;
	        $return['status'] = 1;
	        $this->ajaxReturn($return);
	    }elseif($coin_address&&$coin_address['eth_address']&&$type==3){
	        $data['url'] = $coin_address['eth_address'];
	        $data['ercode'] = $coin_address['eos_ercode'];
	        $return['data'] = $data;
	        $return['status'] = 1;
	        $this->ajaxReturn($return);
	    }elseif(!$coin_address['btc_address']&&$type==1){
	        //echo 3;
	        include_once("AUTO_Z/Easycoin.class.php") ;
	        $QIANBAO = new \Easycoin('root','password');
	        $address = $QIANBAO->getnewaddress($uid);
	        if($address){
	        	$currency=M('Currency')->where('currency_id = 1')->find();
	        	$ercode=$this->qrcode($address, $currency['currency_logo'], $this->uid.$currency['currency_mark']);
	            $data_coin['btc_address']     = $address;
	            $data_coin['btc_ercode']     = $ercode;
	            M('Wallet')->where(['uid'=>$uid])->save($data_coin);
	        }
	        $data['url'] = $address;
	        $data['ercode'] = $ercode;
	        $return['data'] = $data;
	        $return['status'] = 1;
	        $this->ajaxReturn ($return);
	        //如果点进来的是eth 数据库没有 生成eth地址
	    }elseif(!$coin_address['eth_address']&&($type==2||$type==3)){
	        //echo 4;
	        $miyao = $coin_address['miyao'];
	        $url="http://39.104.56.81:8080/ethapi/createAddress.do?password=".$miyao;
	        $result=$this->curl_get($url);
	        if($result['data']&&$result['code']==200){
	        	$currency2=M('Currency')->where('currency_id = 2')->find();
	        	$ercode2=$this->qrcode($result['data'], $currency2['currency_logo'], $this->uid.$currency2['currency_mark']);
	        	$currency3=M('Currency')->where('currency_id = 3')->find();
	        	$ercode3=$this->qrcode($result['data'], $currency3['currency_logo'], $this->uid.$currency3['currency_mark']);
	            $data_coin['eth_address']     = $result['data'];
	            $data_coin['eth_ercode']     = $ercode2;
	            $data_coin['eos_ercode']     = $ercode3;
	            M('Wallet')->where(['uid'=>$uid])->save($data_coin);
	        }
	        $data['url'] = $result['data'];
	        if($type==2){
	        	$data['ercode']=$ercode2;
	        }else if($type==3){
	        	$data['ercode']=$ercode3;
	        }
	        $return['data'] = $data;
	        $return['status'] = 1;
	        $this->ajaxReturn ($return);	        
	    }
	}
	public function addTradeLog($member_id,$type,$money,$inc_money,$url){
	    $data['member_id']=$member_id;
	    $data['type']=$type;
	    $data['money']=$money;
	    $data['inc_money']=$inc_money;
	    $data['url']=$url;
	    $data['add_time'] = time();
	    $data['ip'] = get_client_ip();
	    $list=M('Trade_log')->add($data);
	    if($list){
	        return $list;
	    }else{
	        return false;
	    }
	}
	//获取交易记录
	public function getTradeLog(){
	    $where['tl.member_id'] = $this->uid;
	    $where['tl.type'] = I('type');
	    $list=M('Trade_log tl')
	    ->where($where)
	    ->join('join yang_currency1 c on tl.type = c.currency_id')
	    ->select();
	    $data['status']=1;
	    $data['data']=$list;
	    $this->ajaxReturn($data);
	}
	//获取交易记录
	public function getChongbiLog(){
	    $where['cp.uid'] = $this->uid;
	    $where['cp.coin_type'] = I('type');
	    $list=M('Coin_pay cp')
	    ->where($where)
	    ->join('join yang_currency1 c on cp.coin_type = c.currency_id')
	    ->select();
	    $data['status']=1;
	    $data['data']=$list;
	    $this->ajaxReturn($data);
	}
	//提币获取手续费
	public function getFee(){
	    $type = I('type')?I('type'):'';
	    $num = I('num')?I('num'):'';
	    if(!$type||!$num){
	        $data['status']=-1;
	        $data['info']='参数错误';
	        $this->ajaxReturn($data);
	    }
	    $usermoney = M('Member_money')->where(['member_id'=>$this->uid])->find();
	    if($usermoney['money'.$type]<$num){
	        $data['status']=-2;
	        $data['info']='余额不足';
	        $this->ajaxReturn($data);
	    }
	    //VIC价格
	    $price = $this->getConfig()['vic_price'];
	   /*  $currency = M('Currency1')->where(['currency_id'=>$type])->find();
	    //以太坊比特币PAI最新价
	    $url="https://api.huobi.pro/market/detail/merged?symbol=".$currency['currency_mark']."usdt";
	    $result=$this->curl_get($url);
	    $usd_price = $result['tick']['close']; */
	    $usd_price = $this->getPrice()['price'.$type];
	    //提币手续费
	    $fee = $this->getConfig()['fee'];
	    //提币价值多少美金
	    $usd_price_zong = $usd_price*$num;
	    //扣除多少vic
	    $fee_money = $usd_price_zong*$fee/$price;
	    //查询本人有多少VIC
	    $user_vic = M('Member_money')->where(['member_id'=>$this->uid])->find()['money1'];
	    $vic_log = M('Project_log')->where(['uid'=>$this->uid,'type'=>1])->sum('money');
	    //手续费五折
	    if($user_vic+$vic_log>=5000){
	        $fee_money = $usd_price_zong*$fee*0.5/$price;
	    }
	    if($usermoney['money1']<$fee_money){
	        $data['status']=-3;
	        $data['info']='您的VIC余额不足';
	        $this->ajaxReturn($data);
	    }
	    $data['status']=1;
	    $data['data']=$fee_money;
	    $this->ajaxReturn($data);
	}
	public function qrcode($address,$logo1,$name) {
		Vendor('phpqrcode.phpqrcode');//这个很重要，没引用则不能使用
		$value = $address; //二维码内容
		$errorCorrectionLevel = 'L'; //容错级别
		$matrixPointSize = 4; //生成图片大小
		//生成二维码图片
		\QRcode::png($value, 'qrcode.jpg', $errorCorrectionLevel, $matrixPointSize, 2);//之前做的时候少了一个'\'，会显示没有此类
		$logo = '.'.$logo1; //准备好的logo图片 ；引用文件地址同样很重要，我犯了引入地址错误，结果最后的二维码中间没有图片
		$QR = 'qrcode.jpg'; //已经生成的原始二维码图
		if ($logo !== FALSE) {
			$QR = imagecreatefromstring(file_get_contents($QR));
			$logo = imagecreatefromstring(file_get_contents($logo));
			$QR_width = imagesx($QR);//二维码图片宽度
			$QR_height = imagesy($QR);//二维码图片高度
			$logo_width = imagesx($logo);//logo图片宽度
			$logo_height = imagesy($logo);//logo图片高度
			$logo_qr_width = $QR_width / 5;
			$scale = $logo_width/$logo_qr_width;
			$logo_qr_height = $logo_height/$scale;
			$from_width = ($QR_width - $logo_qr_width) / 2;
			//重新组合图片并调整大小
			imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
			$logo_qr_height, $logo_width, $logo_height);
		}
		$url= './ercode/'.$name.'.png';
		//输出图片
		imagepng($QR, $url);
		return substr($url, 1);	
	}
}